﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Configuration;
using WFNetLib;
using Microsoft.Office.Interop.Excel;

namespace TradeMarkSys
{
    public partial class FrmQueryTrademark : Form
    {
        public FrmQueryTrademark()
        {
            InitializeComponent();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                string where = " where 1=1 ";
                if (!txtRegisterNumber.Text.Equals(""))
                {
                    where += string.Format(" and BaseInfo like '%{0}%'", txtRegisterNumber.Text);
                }
                if (!txtRegCode.Text.Equals(""))
                {
                    where += string.Format(" and RegCode like '%{0}%'", txtRegCode.Text);
                }
                if (!txtApplicant.Text.Equals(""))
                {
                    where += string.Format(" and Applicant like '%{0}%'", txtApplicant.Text);
                }
                if (!txtTradeMarkName.Text.Equals(""))
                {
                    where += string.Format(" and TradeMarkName like '%{0}%'", txtTradeMarkName.Text);
                }
                if (!cmbApplyProject.Text.Equals(""))
                {
                    where += string.Format(" and ApplyProject = '{0}'", cmbApplyProject.Text);
                }
                where += "and (ApplyDate Between #" + dateTimePickerStart.Value.ToString("yyyy-MM-dd") + "# and #" + dateTimePickerEnd.Value.ToString("yyyy-MM-dd") + "#)";
                string sql = string.Format(@"select * from TradeMark {0}", where);
                string connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+System.Windows.Forms.Application.StartupPath + "\\TradeMarkSys.mdb";
                OleDbConnection conn = new OleDbConnection(connstr);
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                conn.Open();
                OleDbDataAdapter da = new OleDbDataAdapter();
                da.SelectCommand = cmd;
                DataSet ds = new DataSet();
                da.Fill(ds);
                dataGridViewTrademark.DataSource = ds.Tables[0];
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("对不起系统出现错误");
            }
            label9.Text = dataGridViewTrademark.Rows.Count.ToString() + "件";
        }

        private void dataGridViewTrademark_DoubleClick(object sender, EventArgs e)
        {
            OleDbConnection conn = null;
            try
            {
                var row = dataGridViewTrademark.CurrentRow;
                if (row != null)
                {
                    var id = dataGridViewTrademark.CurrentRow.Cells[0].Value;
                    FrmApproval fa = new FrmApproval();
                    fa.TradeMarkID = id;
                    fa.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("对不起系统出现错误");
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
            btnQuery_Click(null, null);
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void FrmQueryTrademark_Load(object sender, EventArgs e)
        {
            dateTimePickerEnd.Value = DateTime.Now;
            dateTimePickerStart.Value = new DateTime(2010,1,1);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridViewTrademark.Rows.Count == 0)
            {
                MessageBox.Show("没有用于导出的数据");
                return;
            }
            ExcelExport ExcelEx = new ExcelExport(dataGridViewTrademark.Rows.Count);
            ExcelEx.ExcelWorkbookCallbackProc = new ExcelExport.ExcelWorkbookCallback(ExcelWorkbookCallbackProc);
            ExcelEx.ExcelExportProc();
        }        
        private bool ExcelWorkbookCallbackProc(Workbook wBook, int index)
        {
            Worksheet wSheet;
            wSheet = (Worksheet)wBook.Worksheets[1];
            Range dr;
            if (index == -1)
            {
                for (int i = 0; i < wBook.Worksheets.Count - 1; i++)
                {
                    wSheet = (Worksheet)wBook.Worksheets[i + 1];
                    wSheet.Delete();
                }
                wSheet = (Worksheet)wBook.Worksheets[1];
                wSheet.Name = "商标导出数据";
                wSheet.Cells[1, 1] = "序号";
                wSheet.Cells[1, 2] = "申请时间";
                wSheet.Cells[1, 3] = "登记号";
                wSheet.Cells[1, 4] = "注册号";
                wSheet.Cells[1, 5] = "类别";
                wSheet.Cells[1, 6] = "商标名称";
                wSheet.Cells[1, 7] = "申请项目";
                wSheet.Cells[1, 8] = "申请人";
                wSheet.Cells[1, 9] = "地址";
                wSheet.Cells[1, 10] = "联系人";
                wSheet.Cells[1, 11] = "电话";
                dr = wSheet.get_Range("A1", "K1");
                dr.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DarkOrange);
                dr.Interior.Pattern = XlPattern.xlPatternSolid;
            }
            else if (index == int.MaxValue)
            {
                dr = wSheet.get_Range("A1", "K" + (dataGridViewTrademark.Rows.Count + 1).ToString());
                dr.Columns.AutoFit();
                dr.HorizontalAlignment = XlHAlign.xlHAlignCenter;
                dr.Borders.LineStyle = XlLineStyle.xlContinuous;
            }
            else
            {
                System.Data.DataTable dt = dataGridViewTrademark.DataSource as System.Data.DataTable;
                DataRow ex = dt.Rows[index];
                wSheet.Cells[2 + index, 1] = (index+1).ToString();
                wSheet.Cells[2 + index, 2] = ex["ApplyDate"].ToString();
                wSheet.Cells[2 + index, 3] = ex["BaseInfo"].ToString();
                wSheet.Cells[2 + index, 4] = ex["RegCode"].ToString();
                wSheet.Cells[2 + index, 5] = ex["KindCode"].ToString();
                wSheet.Cells[2 + index, 6] = ex["TradeMarkName"].ToString();
                wSheet.Cells[2 + index, 7] = ex["ApplyProject"].ToString();
                wSheet.Cells[2 + index, 8] = ex["Applicant"].ToString();
                wSheet.Cells[2 + index, 9] = ex["Address"].ToString();
                wSheet.Cells[2 + index, 10] = ex["CommissionFee"].ToString();
                wSheet.Cells[2 + index, 11] = ex["Tel"].ToString();
                if (index % 2 == 1)
                {
                    dr = wSheet.get_Range("A" + (2 + index).ToString(), "K" + (2 + index).ToString());
                    dr.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray);
                    dr.Interior.Pattern = XlPattern.xlPatternSolid;
                }
            }
            return true;
        }
    }
}
